package com.itheima.d1_exception;

import java.util.Arrays;

/**
 * 目标：掌握自定义异常，以及异常的作用。
 */
public class ExceptionTest4 {
    public static void main(String[] args) {
        // 需求：保存一个合法的年龄，如果年龄不合法抛出年龄不合法异常
        Student s1 = new Student();
        System.out.println("s1 = " + s1);
        try {
            Student s2 = new Student("张三", 149);
            System.out.println(s2.getName()+s2.getAge());
        } catch (AgeException e) {
            e.printStackTrace();
        }

    }


}
class  Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) throws AgeException {
        this.name = name;
        //this.age = age;
        if(age >=0&&age<=150){
            this.age = age;
        }else {
            throw new AgeException("年龄数据非法，必须在0到150之间");
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

public void setName(int age) throws AgeException{
    if(age>0&&age<=150){
        this.age =age;
    }else {
        throw new AgeException("年龄不合法，必须在1-150之间");
    }
        }
}

class AgeException extends Exception{
    //2 构造方法补上

    public AgeException() {
    }

    public AgeException(String message) {
        super(message);
    }

    public AgeException(String message, Throwable cause) {
        super(message, cause);
    }

    public AgeException(Throwable cause) {
        super(cause);
    }

    public AgeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
